//
//  SetViewController.m
//  Diary
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "SetViewController.h"
#import "SetTableViewCell.h"
#import "PasswordViewController.h"
#import "AppDelegate.h"
#import "PhotoDiary.h"
#import "PhotoWrite.h"
#import "PhotoDetailed.h"
#import "PhotoMood.h"
#import "PhotoPasswordViewController.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    
    NSArray *groupArray;
    NSArray *contentArray;
    NSIndexPath *indexPath1;
}


@end

@implementation SetViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
     self.view.backgroundColor = [UIColor colorWithRed:10/255.0 green:150/255.0 blue:100/255.0 alpha:0.5];
    NSArray *array1 = @[@"日记列表背景",@"日记写作背景",@"日记详细背景",@"心情背景"];
    NSArray *array2 = @[@"密码",@"照片密码"];
    NSArray *array3 = @[@"关于"];
    
    groupArray = @[array1,array2,array3];
    
    UITableView *tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenFrame_Widdth, ScreenFrame_Height) style:UITableViewStyleGrouped];
    tabelView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tabelView];
    
    tabelView.dataSource = self;
    tabelView.delegate = self;
    
    
    
    passwordTag = NO;
    passwordPhotoTag = NO;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return groupArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [groupArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"SetTableViewCell";
    SetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SetTableViewCell" owner:self options:nil]firstObject];
    }
    
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"];
    NSInteger myPhotoInteger = [userDefaultes integerForKey:@"myPhotoInteger"];
   
    cell.setLabel.text = groupArray[indexPath.section][indexPath.row];
    
    if (indexPath.section==0 && indexPath.row==0) {
        
        [cell.passwordSw addTarget:self action:@selector(setDiaryOfBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    
    if (indexPath.section==0 && indexPath.row==1) {
        
        [cell.passwordSw addTarget:self action:@selector(setWriteOfBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    
    if (indexPath.section==0 && indexPath.row==2) {
        
        [cell.passwordSw addTarget:self action:@selector(setDetailedOfBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    
    if (indexPath.section==0 && indexPath.row==3) {
        
        [cell.passwordSw addTarget:self action:@selector(setMoodOfBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    
    
    if (indexPath.section==1 && indexPath.row==0) {
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (myInteger==100) {
            //设置SW为开
            [cell.passwordSw setOn:YES animated:YES];
            passwordTag = YES;
        }
        //设置密码
        [cell.passwordSw addTarget:self action:@selector(setPassword) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    if (indexPath.section==1 && indexPath.row==1) {
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        if (myPhotoInteger==1000) {
            //设置SW为开
            [cell.passwordSw setOn:YES animated:YES];
            passwordPhotoTag = YES;
        }
        //设置密码
        [cell.passwordSw addTarget:self action:@selector(setPhotoPassword) forControlEvents:UIControlEventTouchUpInside];
    }

    
    
    if (indexPath.section==2) {
        
        [cell.passwordSw addTarget:self action:@selector(setAboutOfBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==1 && indexPath.row==0) {
        
        PasswordViewController *passwordVC = [[PasswordViewController alloc] init];
        [self.navigationController pushViewController:passwordVC animated:YES];
        
    }
    
    if (indexPath.section==1 && indexPath.row==1) {
        
        PhotoPasswordViewController *photoPasswordVC = [[PhotoPasswordViewController alloc] init];
        [self.navigationController pushViewController:photoPasswordVC animated:YES];
        
    }
    
    
}


- (void)setDiaryOfBackground{
    
    isTag = 100;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    // 设置挑选出的图片是否可编辑
    picker.allowsEditing = YES;
    // 模态出图片库
    [self presentViewController:picker animated:YES completion:nil];
    
    
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
   UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    
    //日记列表
    if (isTag==100) {
        
        //在添加新的照片之前删除原来的
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"PhotoDiary"];
        NSPredicate *cata = [NSPredicate predicateWithFormat:@"number>0"];//获取当前行的内容(数据库中的)
        request.predicate = cata;
        NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];
        
        for (PhotoDiary *photo in array) {
            [app.managedObjectContext deleteObject:photo];
        }
        
        if ([app.managedObjectContext hasChanges]) {
            [app.managedObjectContext save:nil];
        }
        
        
        //添加新的照片
        self.imageData = UIImageJPEGRepresentation(image, 1);
        PhotoDiary *photo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoDiary" inManagedObjectContext:app.managedObjectContext];
        photo.photo = self.imageData;
        
        photo.number = [NSString stringWithFormat:@"%@",self.imageData];
        
        //保存数据
        [app saveContext];

    }
    
    //日记写作
    if (isTag==101) {
        
        //在添加新的照片之前删除原来的
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"PhotoWrite"];
        NSPredicate *cata = [NSPredicate predicateWithFormat:@"number>0"];//获取当前行的内容(数据库中的)
        request.predicate = cata;
        NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];
        
        for (PhotoWrite *photo in array) {
            [app.managedObjectContext deleteObject:photo];
        }
        
        if ([app.managedObjectContext hasChanges]) {
            [app.managedObjectContext save:nil];
        }
        
        
        //添加新的照片
        self.imageData = UIImageJPEGRepresentation(image, 1);
        PhotoWrite *photo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoWrite" inManagedObjectContext:app.managedObjectContext];
        photo.photo = self.imageData;
        
        photo.number = [NSString stringWithFormat:@"%@",self.imageData];
        
        //保存数据
        [app saveContext];
        
    }

    
    //日记详细
    if (isTag==102) {
        
        //在添加新的照片之前删除原来的
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"PhotoDetailed"];
        NSPredicate *cata = [NSPredicate predicateWithFormat:@"number>0"];//获取当前行的内容(数据库中的)
        request.predicate = cata;
        NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];
        
        for (PhotoDetailed *photo in array) {
            [app.managedObjectContext deleteObject:photo];
        }
        
        if ([app.managedObjectContext hasChanges]) {
            [app.managedObjectContext save:nil];
        }
        
        
        //添加新的照片
        self.imageData = UIImageJPEGRepresentation(image, 1);
        PhotoDetailed *photo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoDetailed" inManagedObjectContext:app.managedObjectContext];
        photo.photo = self.imageData;
        
        photo.number = [NSString stringWithFormat:@"%@",self.imageData];
        
        //保存数据
        [app saveContext];
        
    }

    //心情
    if (isTag==103) {
        
        //在添加新的照片之前删除原来的
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"PhotoMood"];
        NSPredicate *cata = [NSPredicate predicateWithFormat:@"number>0"];//获取当前行的内容(数据库中的)
        request.predicate = cata;
        NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];
        
        for (PhotoMood *photo in array) {
            [app.managedObjectContext deleteObject:photo];
        }
        
        if ([app.managedObjectContext hasChanges]) {
            [app.managedObjectContext save:nil];
        }
        
        
        //添加新的照片
        self.imageData = UIImageJPEGRepresentation(image, 1);
        PhotoMood *photo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoMood" inManagedObjectContext:app.managedObjectContext];
        photo.photo = self.imageData;
        
        photo.number = [NSString stringWithFormat:@"%@",self.imageData];
        
        //保存数据
        [app saveContext];
        
    }

    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
//设置日记写作背景
- (void)setWriteOfBackground{
    
    isTag = 101;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    // 设置挑选出的图片是否可编辑
    picker.allowsEditing = YES;
    // 模态出图片库
    [self presentViewController:picker animated:YES completion:nil];
    
    
}

//设置日记详细背景
- (void)setDetailedOfBackground{
    
    isTag = 102;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    // 设置挑选出的图片是否可编辑
    picker.allowsEditing = YES;
    // 模态出图片库
    [self presentViewController:picker animated:YES completion:nil];
}

//设置心情背景
- (void)setMoodOfBackground{
    
    isTag = 103;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    // 设置挑选出的图片是否可编辑
    picker.allowsEditing = YES;
    // 模态出图片库
    [self presentViewController:picker animated:YES completion:nil];
}


//设置密码的状态
- (void)setPassword{
      if (passwordTag == NO) {
        passwordTag = YES;
        
        int myInteger = 100;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setInteger:myInteger forKey:@"myInteger"];

    }
    else{
        passwordTag = NO;
        int myInteger = 200;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setInteger:myInteger forKey:@"myInteger"];
        
    }
    
}

//这这照片密码状态
- (void)setPhotoPassword{
    
    if (passwordPhotoTag == NO) {
        passwordPhotoTag = YES;
        
        int myPhotoInteger = 1000;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setInteger:myPhotoInteger forKey:@"myPhotoInteger"];
        
    }
    else{
        passwordPhotoTag = NO;
        int myPhotoInteger = 2000;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setInteger:myPhotoInteger forKey:@"myPhotoInteger"];
        
    }

    
}




//关于
- (void)setAboutOfBackground{
   
    
    if (aboutTag==NO) {
        aboutTag=YES;
       
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 480, ScreenFrame_Widdth, 200)];
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.contentSize = CGSizeMake(0,550);
        
        if (ScreenFrame_Widdth == 320) {
            
            scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, ScreenFrame_Widdth, 200)];
            scrollView.backgroundColor = [UIColor colorWithRed:10/255.0 green:150/255.0 blue:100/255.0 alpha:1];
            scrollView.contentSize = CGSizeMake(0,450);
        }
        
        textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, ScreenFrame_Widdth, 200)];
        textView.backgroundColor = [UIColor clearColor];
        textView.userInteractionEnabled = NO;
        textView.text = @"阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分阿杜舒服阿凡达分舒服啊舒服 分啊发啊舒服 发送到发货的撒风 萨芬撒的分";
        
      //  自适应textView的高度
        CGFloat height = [self heightForLabelText:textView.text];
        CGRect rect = textView.frame;
        rect.size.height = height;
        textView.frame = rect;
        [scrollView addSubview:textView];
        [self.view addSubview:scrollView];
        
    }
    else{
        
        aboutTag = NO;
        [scrollView removeFromSuperview];
    }
    
}

//自适应高度方法
- (CGFloat)heightForLabelText:(NSString *)text{
    
    //size
    CGSize size = CGSizeMake(ScreenFrame_Widdth - 20, 10000);
    //attributes
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //设置
    return rect.size.height + 40;
}

@end
